package com.sgb.kjwl.view.ui.fragment.manage

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.google.android.material.tabs.TabLayout
import com.sgb.kjwl.AppLazyFragment
import com.sgb.kjwl.R
import com.sgb.kjwl.databinding.FragmentLeaseOrderBinding
import com.sgb.link.entity.PickFilterEntity

class AgentLeaseOrderFragment : AppLazyFragment() {

    private lateinit var mBinding : FragmentLeaseOrderBinding
    private var mTitles : ArrayList<PickFilterEntity<Any>> ?= null
    private lateinit var mAdapter : LeaseOrderPageAdapter

    companion object{
        const val TAG = "agentLeaseOrderFragment"
        const val KEY_DATAS = "key_datas"
        /**
         * 二级标题
         */
        fun newInstance(titles : ArrayList<PickFilterEntity<Any>>)  : AgentLeaseOrderFragment{
            val fragment = AgentLeaseOrderFragment()
            val bundle = Bundle()
            bundle.putSerializable(KEY_DATAS, titles)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (arguments != null){
            mTitles = requireArguments().getSerializable(AgentGoodsOrderFragment.KEY_DATAS) as ArrayList<PickFilterEntity<Any>>?
        }
        if (mTitles == null){
            mTitles = ArrayList()
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_lease_order, container, false)
        mAdapter = LeaseOrderPageAdapter(childFragmentManager, mTitles!!)
        mBinding.viewPage.adapter = mAdapter
        mBinding.viewPage.offscreenPageLimit = 1
        mBinding.viewPage.currentItem = 0
        mBinding.tablayout.setupWithViewPager(mBinding.viewPage)
        mBinding.tablayout.tabMode = TabLayout.MODE_FIXED
        return mBinding.root
    }

    override fun onLazyLoad() {
        //刷新第二级的第一个tab
        val fragment = childFragmentManager.findFragmentByTag(
            "android:switcher:" + mBinding.viewPage.id.toString() + ":" + 0)
        if (fragment != null && fragment is AgentLeaseOrderStateFragment){
            fragment.initData()
        }
    }


    class LeaseOrderPageAdapter(fm : FragmentManager, titles : ArrayList<PickFilterEntity<Any>>) : FragmentPagerAdapter(fm) {
        private var mTitles : ArrayList<PickFilterEntity<Any>> ?= null
        init {
            this.mTitles = titles
        }
        override fun getCount(): Int {
            if (mTitles != null){
                return mTitles!!.size
            }
            return 0
        }

        override fun getItem(position: Int): Fragment {
            var no = mTitles!![position].no
            when(no) {
                "7" -> { //出租订单--待接单、待运输
                    return AgentLeaseOrderStateFragment.newInstance("", "2")
                }
                "8" -> { //租入订单--待确认、待付款、待验收
                    return AgentLeaseOrderStateFragment.newInstance("", "1")
                }
            }
            return OtherInquiryFragment.newInstance()
        }

        override fun getPageTitle(position: Int): CharSequence {
            if (mTitles != null){
                return mTitles!![position].name
            }
            return ""
        }
    }
}